package com.leyou.item.controller;

import com.leyou.item.pojo.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//品牌controller
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父节点id查询类目
     * @param pid
     * @return
     */
    @RequestMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryByParentId(
            @RequestParam(value = "pid",defaultValue = "0") Long pid
    ){
        return ResponseEntity.ok(this.categoryService.queryListByParent(pid));
    }

    /**
     * 添加子节点
     * @return
     */
    @RequestMapping("/of/child")
    public ResponseEntity<Void> addChildNode(@RequestBody CategoryDTO categoryDTO){
           /* node信息
          id: 0   //父节点信息
          isParent: false   //是否是父节点
          name: "新的节点"   //名字
          parentId: 1      //父节点id
          sort: 13*/      //顺序
        System.out.println(categoryDTO);
        categoryService.addChildNode(categoryDTO);
        return ResponseEntity.ok().body(null);
    }

    /**
     * 删除节点
     * @return
     */
    @RequestMapping("/of/delete")
    public ResponseEntity<Void> delete(@RequestParam("id")Long id){

        categoryService.deleteById(id);
        return ResponseEntity.ok().body(null);
    }


    /**
     * /category/of/brand/?id=2032
     * 根据品牌id查询所属的分类
     * @return
     */
    @RequestMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryByBrandId(@RequestParam("id")Long brandId){
        return ResponseEntity.ok(this.categoryService.queryListByBrandId(brandId));
    }

    /**
     * 根据种类id集合查询种类集合
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids")List<Long> idList){
        return ResponseEntity.ok().body(this.categoryService.queryCategoryByIds(idList));
    }



    /**
     * 根据三级分类id查询所有分类信息
     * @return
     */
    @GetMapping("/levels")
    public ResponseEntity<List<CategoryDTO>> queryAllByCid3(@RequestParam("id")Long cid){
        return ResponseEntity.ok(this.categoryService.queryAllByCid3(cid));
    }

}
